package middleware

import (
	"github.com/gin-gonic/gin"
	"interview/pkg/global"
	"time"
)

// AccessLog 访问记录
func AccessLog(c *gin.Context) {
	start := time.Now()
	// 处理请求
	c.Next()
	end := time.Now()
	execTime := end.Sub(start)
	reqMethod, reqUri, statusCode, clientIP := c.Request.Method, c.Request.RequestURI, c.Writer.Status(), c.ClientIP()
	if reqMethod == "OPTIONS" {
		global.Logger.Debugf("%s [%s] %s status %d spent %s\n", clientIP, reqMethod, reqUri, statusCode, execTime.String())
	} else {
		global.Logger.Infof("%s [%s] %s status %d spent %s\n", clientIP, reqMethod, reqUri, statusCode, execTime.String())
	}
}
